package edu.ccut.saturn.studio.dialog.normal;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnNormalDialog extends TitleAreaDialog {

	private Text idText;
	private Text nameText, descriptionText;

	private Label nameLabel, descriptionLabel;

	private Shape shape;

	public SaturnNormalDialog(Shell parentShell, Shape saturnViewerNode) {
		super(parentShell);
		this.shape = saturnViewerNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + shape.getName()); 
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
//		setTitleImage(ResourceManager.getPluginImage(ShapesPlugin.getDefault(), "icons/logic/HDbig.GIF")); //$NON-NLS-1$

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText(Messages.getString("SaturnNormalDialog.ID")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.horizontalIndent = 5;
		gd_idText.verticalIndent = 5;
		idText.setLayoutData(gd_idText);
		idText.setText(shape.getId().trim());
		idText.setSelection(shape.getId().trim().length());

		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 15;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnNormalDialog.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textName.horizontalIndent = 5;
		gd_textName.verticalIndent = 5;
		nameText.setLayoutData(gd_textName);
		nameText.setText(shape.getName().trim());
		nameText.setSelection(shape.getName().trim().length());

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		gd_descriptionLabel.verticalIndent = 15;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnNormalDialog.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textDec.heightHint = 53;
		gd_textDec.horizontalIndent = 5;
		gd_textDec.verticalIndent = 5;
		descriptionText.setLayoutData(gd_textDec);
		descriptionText.setText(shape.getDescription());

		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if ("".equals(nameText.getText().trim())) { 
					setErrorMessage(Messages.getString("SaturnNormalDialog.NameLimit")); //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});

		return area;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			shape.setDescription(descriptionText.getText().trim());
			shape.setName(nameText.getText().trim());
		}

		super.buttonPressed(buttonId);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("ManageConnectionDialog.confirm"), false); //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("ManageConnectionDialog.cancel"), false); //$NON-NLS-1$
	getButton(IDialogConstants.OK_ID).setEnabled(true);
}

	@Override
	protected Point getInitialSize() {
		return new Point(500, 272);
	}

	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
//		newShell.setImage(ResourceManager.getPluginImage(ShapesPlugin.getDefault(), "icons/logic/HDsmall.GIF")); //$NON-NLS-1$
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		newShell.setText(Messages.getString("SaturnNormalDialog.Setparameter")); //$NON-NLS-1$
	}
}